﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common;
using ESOL.WinUI.UICommon;
using ESOL.Common.Entity;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Bpt
{
    public partial class frmManageEncoder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstProductEntity mSelectedProduct;
        List<string> Listcondition = new List<string>();
        List<string> ListProductId = new List<string>();
        #endregion

        #region Constructor
        public frmManageEncoder()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colMainUnit, "UNIT", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSecondaryUnit, "UNIT", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCodeType, "RCT", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCodeType, "RCT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCodeType, "RCT", false);
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtBroadClassName.Text) == false)
            {
                condition += " AND C.PRODUCT_TYPE_NAME LIKE '%" + txtBroadClassName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtBroadClassCode.Text) == false)
            {
                condition += " AND C.PRODUCT_TYPE_CODE LIKE '%" + txtBroadClassCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMiddleClassName.Text) == false)
            {
                condition += " AND B.PRODUCT_TYPE_NAME LIKE '%" + txtMiddleClassName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMiddleClassCode.Text) == false)
            {
                condition += " AND B.PRODUCT_TYPE_CODE LIKE '%" + txtMiddleClassCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                condition += " AND A.TYPE_NAME LIKE '%" + txtTypeName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false)
            {
                condition += " AND A.TYPE_CODE LIKE '%" + txtTypeCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtStandardName.Text) == false)
            {
                condition += " AND A.STD_NAME LIKE '%" + txtStandardName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtStandardCode.Text) == false)
            {
                condition += " AND A.STD_CODE LIKE '%" + txtStandardCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMaterialName.Text) == false)
            {
                condition += " AND A.MATERIAL LIKE '%" + txtMaterialName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMaterialCode.Text) == false)
            {
                condition += " AND A.MATERIAL_CODE LIKE '%" + txtMaterialCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " AND A.PRODUCT_CODE LIKE '%" + txtProductCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductName.Text) == false)
            {
                condition += " AND A.PRODUCT_NAME LIKE '%" + txtProductName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbCodeType.SelectedValue.ToString()) == false)
            {
                condition += "AND A.ROOT_CODE_TYPE = '" + cmbCodeType.SelectedValue.ToString() + "'";
            }
            if (chkIsFlowCode.Checked)
            {
                condition += "AND A.FLOW_CODE IS NOT NULL ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PRODUCT_CODE";

            DataTable dt = MstProductBs.GetByCondition_Code(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindCombobox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedProduct = new MstProductEntity(dr);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedProduct == null)
            {
                MessageBox.Show("请选择一个编码！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEncoder frm = new frmMaintainEncoder();
            frm.ThisActionType = ActionType.View;
            frm.SelectedProduct = mSelectedProduct;
            frm.ShowDialog();
            base.Click_View();
        }

        protected override void Click_New()
        {
            frmMaintainEncoder frm = new frmMaintainEncoder();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedProduct == null)
            {
                MessageBox.Show("请选择一个编码！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEncoder frm = new frmMaintainEncoder();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedProduct = mSelectedProduct;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedProduct == null)
            {
                MessageBox.Show("请选择一个编码！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中编码？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedProduct.DeleteFlag = 1;
                bool successful = MstProductBs.DeleteMstProduct(mSelectedProduct);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Export()
        {
            SaveFileDialog sfd=new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if(sfd.ShowDialog()==DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
        }

        private void txtBroadClassName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "BroadClassName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtBroadClassName.Text = frm.aList[0];
                txtBroadClassCode.Text = frm.aList[1];
            }
            else
            {
                txtBroadClassName.Text = "";
                txtBroadClassCode.Text = "";
            }
        }

        private void txtBroadClassCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "BroadClassCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtBroadClassName.Text = frm.aList[0];
                txtBroadClassCode.Text = frm.aList[1];
            }
            else
            {
                txtBroadClassCode.Text = "";
                txtBroadClassName.Text = "";
            }
        }

        private void txtMiddleClassName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MiddleClassName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMiddleClassName.Text = frm.aList[0];
                txtMiddleClassCode.Text = frm.aList[1];
            }
            else
            {
                txtMiddleClassName.Text = "";
                txtMiddleClassCode.Text = "";
            }
        }

        private void txtMiddleClassCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MiddleClassCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMiddleClassName.Text = frm.aList[0];
                txtMiddleClassCode.Text = frm.aList[1];
            }
            else
            {
                txtMiddleClassName.Text = "";
                txtMiddleClassCode.Text = "";
            }
        }

        private void txtTypeName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "TypeName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtTypeName.Text = frm.aList[0];
                txtTypeCode.Text = frm.aList[1];
            }
            else
            {
                txtTypeName.Text = "";
                txtTypeCode.Text = "";
            }
        }

        private void txtTypeCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "TypeCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtTypeName.Text = frm.aList[0];
                txtTypeCode.Text = frm.aList[1];
            }
            else
            {
                txtTypeName.Text = "";
                txtTypeCode.Text = "";
            }
        }

        private void txtStandardName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "StdName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtStandardName.Text = frm.aList[0];
                txtStandardCode.Text = frm.aList[1];
            }
            else
            {
                txtStandardName.Text = "";
                txtStandardCode.Text = "";
            }
        }

        private void txtStandardCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "StdCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtStandardName.Text = frm.aList[0];
                txtStandardCode.Text = frm.aList[1];
            }
            else
            {
                txtStandardName.Text = "";
                txtStandardCode.Text = "";
            }
        }

        private void txtMaterialName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MaterialName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMaterialName.Text = frm.aList[0];
                txtMaterialCode.Text = frm.aList[1];
            }
            else
            {
                txtMaterialName.Text = "";
                txtMaterialCode.Text = "";
            }
        }

        private void txtMaterialCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MaterialCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMaterialName.Text = frm.aList[0];
                txtMaterialCode.Text = frm.aList[1];
            }
            else
            {
                txtMaterialName.Text = "";
                txtMaterialCode.Text = "";
            }
        }

        private void txtProductName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            Listcondition.Add(txtProductName.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "ProductCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtProductName.Text = frm.aList[0];
                txtProductCode.Text = frm.aList[1];
            }
            else
            {
                txtProductName.Text = "";
                txtProductCode.Text = "自动生成";
            }
        }

        #endregion

    }
}
